package svc

import (
	"context"
	"github.com/zeromicro/go-zero/zrpc"
	"go-exp/pkg/orm"
	"go-exp/pkg/redis"
	"go-exp/query"
	"go-exp/zero/rpc/permission/internal/config"
	"go-exp/zero/rpc/user/userrpc"
)

type ServiceContext struct {
	Config  config.Config
	UserRpc userrpc.UserRpc
	Redis   *redis.Redis
	DB      *orm.DB
}

func NewServiceContext(c config.Config) *ServiceContext {
	NewRedis := redis.MustNewRedis(context.Background(), &redis.Conf{
		Ip:       c.RedisConf.Host,
		Password: c.RedisConf.Password,
		Db:       c.RedisConf.Db,
		Port:     c.RedisConf.Port,
	})
	db := orm.MustNewMysql(&orm.Config{DSN: c.DB.DataSource})
	query.SetDefault(db.DB)
	return &ServiceContext{
		Config:  c,
		UserRpc: userrpc.NewUserRpc(zrpc.MustNewClient(c.UserRpc)),
		Redis:   NewRedis,
		DB:      db,
	}
}
